package com.meiya.lab.hbase.crud;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseTestCase {
	static Configuration cfg = null;
	static {
		cfg = HBaseConfiguration.create();
		cfg.set("hbase.zookeeper.quorum", "192.168.1.104");
		cfg.set("hbase.zookeeper.property.clientPort", "2181");
	}

	/**
	 * 创建一张表
	 */
	public static void creatTable(String tablename) throws Exception {
		HBaseAdmin admin = new HBaseAdmin(cfg);
		if (admin.tableExists(tablename)) {
			System.out.println("table   Exists!!!");
		} else {
			HTableDescriptor tableDesc = new HTableDescriptor(tablename);
			tableDesc.addFamily(new HColumnDescriptor("name:"));
			admin.createTable(tableDesc);
			System.out.println("create table ok .");
		}

	}

	public static void writeRow(String tablename, String[] cfs) {
		try {
			HTable table = new HTable(cfg, "test");
			Put put = new Put(Bytes.toBytes("rows1"));
			for (int j = 0; j < cfs.length; j++) {
				put.add(Bytes.toBytes(cfs[j]),
						Bytes.toBytes(String.valueOf(1)),
						Bytes.toBytes("value_1"));
				table.put(put);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] agrs) {
		try {
			HTable table = new HTable(cfg, "test");
			Put put = new Put(Bytes.toBytes("rows1"));
			put.add(Bytes.toBytes("colfam1"),
					Bytes.toBytes("qual1"),
					Bytes.toBytes("value_1"));
			table.put(put);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
